 

//import java.applet.Applet;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;
import java.util.Vector;

import org.apache.xmlrpc.XmlRpcException;
import org.neos.client.NeosJobXml;
import org.neos.client.NeosXmlRpcClient;
import org.neos.client.ResultCallback;
import org.neos.client.ResultReceiver;

public class MySimApp {
	
	public static final String RETRIEVING = "retrieving";
	public static final String ERROR = "error";
	public static final String DONE = "done";
	private String status;
	
	private static final String HOST = "www.neos-server.org";
	private static final String PORT = "3332";
	private NeosXmlRpcClient client;
	private ResultReceiver reciever;
	private Integer currentJob = 0;
	private String currentPassword = "";
	private ResultCallback callback;
	private Exception e;
	private StringBuffer currentResult = new StringBuffer();

public String submitJob(String gmscode) {
		
		String ret="";
		//String gmscode = "$title Final Project Parking Lot\noption optcr=0.0;\nfree variable Z;\n*Start parkinglots\nset N \"Nodes(src lots)\" / L0*L5 /;\nset P(N) \"parking lots\";\nP(N) = yes;\nP(\'L0\') = no;\n\n\nparameter w1,w2,w3,npts;\n* w1 for fee, w2 for driving dist, w3 for walking dist\nw1 = 1;\nw2 = 1;\nw3 = 1;\n* n candidates\nnpts = 3;\n\nalias(N,i,j);\nparameter F(N);\nF(i) = round(10*uniform(2,5)+5);\nparameter D(i,j);\nD(i,j) = round(10*uniform(2,5));\nparameters W(N);\nW(P) = round(3*uniform(2,5));\n\n\nset Arc(N,N);\nArc(i,j) = yes;\n\nbinary variable Y(N);\nbinary variable X(N,N);\nnonnegative variable R(N,N);\nfree variable S(N);\nequations\n         obj\n         nLots\n         startpts\n         balance(N)\n         cons1(N)\n         cons2(N)\n         cons3(N)\n         cons4(N)\n         cons5(N,N)\n         cons6(N,N)\n         cons7(N)\n         cons8\n;\n\nobj..\nZ =e= w1*sum(P,F(P)*Y(P)) + w2*sum((i,j)$Arc(i,j),D(i,j)*R(i,j))+w3*sum(P,W(P)*Y(P));\n\nnLots..\nsum(P,Y(P)) =e= npts;\n\nstartpts..\nY(\'L0\') =e= 1;\n\nbalance(i)..\nsum(j$Arc(j,i),R(j,i))+S(i) =e= sum(j$Arc(i,j),R(i,j));\n\ncons1(i)..\nsum(j$Arc(i,j),X(i,j)) =l= 1;\n\ncons2(i)..\nsum(j$Arc(j,i),X(j,i)) =l= 1;\n\ncons3(i)..\nY(i) =g= (sum(j$Arc(i,j),X(i,j))+sum(j$Arc(j,i),X(j,i)))/2;\n\ncons4(i)..\nY(i) =l= (sum(j$Arc(i,j),X(i,j))+sum(j$Arc(j,i),X(j,i)));\n\ncons5(i,j)..\nR(i,j)/npts =l= X(i,j);\n\ncons6(i,j)..\nR(i,j) =g= X(i,j);\n\ncons7(i)$(ord(i) gt 2)..\nS(i) =e= -1*Y(i);\n\ncons8..\nS(\'L0\') =e= npts;\n\nmodel hw /\n         obj\n         nLots\n         startpts\n         balance\n         cons1\n         cons2\n         cons3\n         cons4\n         cons5\n         cons6\n         cons7\n         cons8\n/;\nsolve hw using mip minimizing Z ;\noption X:0:0:1 ;\noption Y:0:0:1 ;\noption R:0:0:1 ;\noption S:0:0:1 ;\ndisplay X.l,Y.l,R.l,S.l;\n";
		NeosJobXml job = new NeosJobXml("milp", "Gurobi", "GAMS");
		job.addParam("model", gmscode);
		
		Vector params = new Vector();
		// Job xml
		params.add(job.toXMLString());
		
		client = new NeosXmlRpcClient(HOST, PORT);
		try {
			client.connect();
		} catch (XmlRpcException e) {
			System.out.println();
		}
		try {
			Object[] results = (Object[]) client.execute("submitJob", params);

			currentJob = (Integer) results[0];
			currentPassword = (String) results[1];
		} catch (XmlRpcException e) {
			System.out.println("Error submitting job :" + e.getMessage());
			return ret;
		}
		if (currentJob == 0) {
			System.out.println(currentPassword);
			return ret;
		}
		
		
		
		
		Vector rparams = new Vector();
		rparams.add(this.currentJob);
		rparams.add(this.currentPassword);

		Vector offsetParams = new Vector();
		offsetParams.add(this.currentJob);
		offsetParams.add(this.currentPassword);

		offsetParams.add(Integer.valueOf(0));
		String neosStatus;
		
		try {
			neosStatus = (String) client.execute("getJobStatus", rparams);
			
			this.status = RETRIEVING;
			while (!neosStatus.equals("Done")) {
				if (neosStatus.equals("Unknown Job")
						|| neosStatus.equals("Bad Password")) {
					this.status = ERROR;
					e = new Exception(neosStatus);
					return ret;
				}

				Object[] retval = (Object[]) client.execute(
						"getIntermediateResults", offsetParams);
				offsetParams.set(2, (Integer) retval[1]);
				Object output = (Object) retval[0];
				String newText;
				if (output instanceof String)
					newText = (String) output;
				else
					newText = new String((byte[]) output);
				currentResult.append(newText);

				neosStatus = (String) client.execute("getJobStatus", rparams);
			}

			Object retval = client.execute("getFinalResults", rparams);

			currentResult.setLength(0);
			if (retval instanceof String) {
				currentResult.append((String) retval);
			} else if (retval instanceof byte[]) {
				currentResult.append(new String((byte[]) retval));

			}
			// parent.tabs.setText(jobNumber, new String();
			this.status = DONE;
			
			System.out.println(currentResult.toString());
		} catch (XmlRpcException e) {
			this.e = e;
			this.status = ERROR;
			System.out.println(e.toString());
		}
		
		if (callback != null) {
			callback.handleFinalResult(currentResult.toString());
		}
		return currentResult.toString();
	}
	

}
